'use strict';
/**
 * @name 账户基金账户页面Controller
 * @author zyf
 */
angular.module('metalabPartnerApp')
    .controller('InfoCtrl', ['$rootScope', '$scope', '$stateParams', '$state', 'MetaResources', 'MetaRemind', 'MetaSEO', 'MetaConstants',
        function ($rootScope, $scope, $stateParams, $state, MetaResources, MetaRemind, MetaSEO, MetaConstants) {
            MetaSEO.setSEO('账户信息', '迈科技账户信息', '迈科技,迈科技账户,迈坦');

            $scope.formData = {};
            // $scope.profileFormData = {};

            $scope.provinces = MetaConstants.provinces;
            $scope.demandFields = MetaConstants.demandFields;

            // $scope.formData = angular.copy($rootScope.USER);
            MetaResources.user.get({
                id:$rootScope.USER.id
            },function(result){
                // console.log('result',result);
                $scope.formData=result;
            });
            // $scope.profileFormData = angular.copy($rootScope.USER);

            $scope.$watch('temp.avatar', function () {
                $scope.formData.avatar = $scope.temp.avatar.link;
            }, true);

            //MetaResources.user.get({id: '_self'}, function (data) {
            //    $scope.user = data;
            //    $scope.formData = angular.copy($scope.user);
            //    $scope.$watch('temp.avatar', function () {
            //        $scope.formData.avatar = $scope.temp.avatar.link;
            //    }, true);
            //
            //}, function (error) {
            //    MetaRemind.error(error);
            //});


            $scope.submitForm = function (valid) {
                if (valid) {
                    //删除文件
                    if (!$scope.formData.partnerInfo.resume) {
                        $scope.formData.partnerInfo.resume = {
                            link: null,
                            name: null,
                            size: 0,
                            type: null
                        };
                    }
                    //更新信息
                    MetaResources.user.update({id: '_self'}, $scope.formData, function (data) {
                        MetaRemind.success('修改成功');
                        $rootScope.USER = data;
                        $state.reload();
                    }, function (error) {
                        MetaRemind.error(error);
                    });
                } else {
                    $scope.clicked = true;
                    MetaRemind.error('请按照表单要求填写');
                }
            };

            $scope.reset = function (flag) {
                $scope.clicked = false;
                flag ? $scope.profileEditing = false : $scope.accountEditing = false;
                $scope.formData = angular.copy($scope.backup);
            };

            $scope.edit = function (flag) {
                flag ? $scope.profileEditing = true : $scope.accountEditing = true;
                $scope.backup = angular.copy($scope.formData);
            };

        }]);
